layui.define(['form','table'], function(exports){
    var $ = layui.$,form = layui.form,table = layui.table;

    table.render({
        elem: '#LAY-table'
        ,url: layui.setter.webRoot + '/agent/referrer/list.data'
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'referrerUserId', title: '用户ID'}
            ,{field: 'name', title: '用户名称' ,templet:function (record){
	         	return record.referrerUser.name;
	          }}
            ,{field: 'phone', title: '用户手机号' ,templet:function (record){
  	         	return record.referrerUser.phone;
  	          }}
            ,{field: 'agentCount', title: '介绍代理人数'}
            ,{title: '操作', width: 200, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
        ]]
        ,page: false
        ,height: 'full-220'
        ,text: '对不起，加载出现异常！'
    });

    //监听搜索
    form.on('submit(LAY-search)', function(data){
        var field = data.field;
        //执行重载
        table.reload('LAY-table', {
            where: field
        });
    });

    var active = {
        info: function(obj){
            layer.open({
                type: 2
                ,title: '查看介绍人'
                ,content: layui.setter.webRoot + '/agent/referrer/info.html?referrerUserId='+obj.data.referrerUserId
                ,maxmin: true
                ,area: ['96%', '96%']
                ,btn: ['取消']
            });
        }
		,profitSyn: function(obj){
		      layer.open({
		        type: 2
		        ,title: '刷新分成'
		        ,content: layui.setter.webRoot + '/agent/referrer/profit/syn.html?referrerUserId='+obj.data.referrerUserId
		        ,maxmin: false
		        ,area: ['600px', '600px']
		        ,btn: ['确定', '取消']
		        ,yes: function(index, layero){
		          var iframeWindow = window['layui-layer-iframe'+ index]
		          ,submitID = 'LAY-form-submit'
		          ,submit = layero.find('iframe').contents().find('#'+ submitID);
		          iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
		            var field = data.field;
		        	$.ajax({
			    		type: "post",
			            url: layui.setter.webRoot + '/agent/referrer/profit/syn.do',
			            data:field,
						dataType:"json",
						success:function(res){
							if(res.code == 200){
								layer.close(index);
								layer.msg('刷新中，请稍后查看',{icon: 1});
							}else{
								layer.msg(res.msg,{icon: 2});
							}
						}
			        });
		          });  
		          submit.trigger('click');
		        }
		      }); 
		    }
    };

    $('.layui-btn.LAY-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    table.on('tool(LAY-table)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });

    exports('agent_referrer_list', {})
});